package stagefour.dao.impl;



import stagefour.dao.UserDao;
import stagefour.model.ProductModel;
import stagefour.model.UserModel;
import stagefour.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class UserDaoImpl implements UserDao {

    @Override
    public Boolean findByUsernameAndPassword(UserModel userModel) {
        String sql = "select * from user where username = ? and password = ?";
        List<Object> list = new ArrayList<>();
        list.add(userModel.getUsername());
        list.add(userModel.getPassword());
        ResultSet resultSet = JDBC.query(sql,list);
        try{
            while (true){
                assert resultSet != null;
                if (!resultSet.next()) break;
                return true;
            }
        }catch (SQLException e){
            e.printStackTrace();
        }

        return false;

    }

    @Override
    public Boolean register(UserModel userModel) {
        String sql = "insert into user values (0,?,?,?,?,0)";
        List<Object> list = new ArrayList<>();
        list.add(userModel.getUsername());
        list.add(userModel.getPassword());
        list.add(userModel.getEmail());
        list.add(userModel.getPhone());
        return JDBC.update(sql, list) > 0;
    }

    @Override
    public List<UserModel> selectUser(String sql, List<Object> list) {

        ResultSet resultSet = JDBC.query(sql, list);

        List<UserModel> userModelList = new ArrayList<UserModel>();
        try{
            while(true) {
                assert resultSet != null;
                if (!resultSet.next()) break;
                UserModel userModel = new UserModel();
                userModel.setUserid(resultSet.getInt("userid"));
                userModel.setUsername(resultSet.getString("username"));
                userModel.setPassword(resultSet.getString("password"));
                userModel.setEmail(resultSet.getString("email"));
                userModel.setPhone(resultSet.getString("phone"));
                userModel.setIsadmin(resultSet.getInt("isadmin"));
                userModelList.add(userModel);
            }
            return userModelList;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public UserModel getUser(Integer userid){
        String sql = "select * from user where userid = ?";
        ResultSet resultSet = JDBC.query(sql, Collections.singletonList(userid));
        try{
            while (true){
                assert resultSet != null;
                if (!resultSet.next()) break;
                UserModel userModel = new UserModel();
                userModel.setUserid(resultSet.getInt("userid"));
                userModel.setUsername(resultSet.getString("username"));
                userModel.setPassword(resultSet.getString("password"));
                userModel.setEmail(resultSet.getString("email"));
                userModel.setPhone(resultSet.getString("phone"));
                userModel.setIsadmin(resultSet.getInt("isadmin"));
                return userModel;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }


    @Override
    public Boolean addUser(UserModel userModel) {
        String sql = "insert into user values (0,?,?,?,?,?)";
        List<Object> list = new ArrayList<Object>();
        list.add(userModel.getUsername());
        list.add(userModel.getPassword());
        list.add(userModel.getEmail());
        list.add(userModel.getPhone());
        list.add(userModel.getIsadmin());

        return JDBC.update(sql,list) > 0;
    }

    @Override
    public Boolean updateUser(UserModel userModel) {
        String sql = "update user set password=?,email=?,phone=?,isadmin=? where userid=?";
        List<Object> list = new ArrayList<Object>();
        list.add(userModel.getPassword());
        list.add(userModel.getEmail());
        list.add(userModel.getPhone());
        list.add(userModel.getIsadmin());
        list.add(userModel.getUserid());

        return JDBC.update(sql,list) > 0;
    }


    @Override
    public Boolean deleteUser(Integer userid) {
        String sql = "delete from user where userid=?";
        List<Object> list = new ArrayList<Object>();
        list.add(userid);

        return JDBC.update(sql,list) > 0;
    }

    @Override
    public List<UserModel> findAll() {
        String sql = "SELECT `userid`,`username`,`email`,`phone`,`isadmin`  FROM user";
        ResultSet resultSet = JDBC.query(sql);
        List<UserModel> list = new ArrayList<>();
        try{
            while (resultSet.next()){
                UserModel userModel = new UserModel();
                userModel.setUserid(resultSet.getInt("userid"));
                userModel.setUsername(resultSet.getString("username"));
                userModel.setEmail(resultSet.getString("email"));
                userModel.setPhone(resultSet.getString("phone"));
                userModel.setIsadmin(resultSet.getInt("isadmin"));
                list.add(userModel);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
